package io.gitee.crud.common;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;

import java.util.Optional;

public class PageContext {

    private static final ThreadLocal<Integer> PAGE_HOLDER = new ThreadLocal<>();
    private static final ThreadLocal<Integer> PAGE_SIZE_HOLDER = new ThreadLocal<>();

    public static <T> Page<T> getMybatisPage() {
        Integer page = Optional.ofNullable(getPage()).orElse(1);
        Integer pageSize = Optional.ofNullable(getPageSize()).orElse(10);
        return new Page<>(page, pageSize);
    }

    public static void setPage(int page) {
        PAGE_HOLDER.set(page);
    }

    public static Integer getPage() {
        return PAGE_HOLDER.get();
    }

    public static void setPageSize(int pageSize) {
        PAGE_SIZE_HOLDER.set(pageSize);
    }

    public static Integer getPageSize() {
        return PAGE_SIZE_HOLDER.get();
    }

    public static void clear() {
        PAGE_HOLDER.remove();
        PAGE_SIZE_HOLDER.remove();
    }

}
